#ifdef MAODV

/*****************************************************************************
 *
 * Copyright (C) 2003 CSHCN, University of Maryland.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Authors: Matthew Impett <m.impett@flarion.com>
 *
 *
 *****************************************************************************/
 
#include "multicast_packet_cache.h"

void	init_packet_cache(void)
{
	int i;
	int j;
	for(i=0; i< HASHBUCKETS; i++)
	{
		packet_cache[i].packet_cache_write_index = 0;
		for(j=0; j < MAX_PACKET_CACHE; j++)
		{
			packet_cache[i].ip_src[j] = 0;
			packet_cache[i].ip_id[j] = 0;
			packet_cache[i].ip_hook[j] = 0;
		}
	}
}

/* This function returns 1 if a duplicate packet has not been
   found and  returns 0 if a duplicate packet has not been found and adds 
   that packet to the packet cache. */
int	check_duplicate(u_int32_t src, u_int16_t id, u_int8_t hook)
{	
	u_int8_t	hash_value;
	int i;
	int j = 0;
	hash_value = 	packet_hashfunction(src);

	for(i=0; i< MAX_PACKET_CACHE; i++)
	{
		if( (packet_cache[hash_value].ip_src[i] == src) &&
                    (packet_cache[hash_value].ip_id[i] == id) &&
		    (packet_cache[hash_value].ip_hook[i] == hook) )
		{
			/* Found a duplicate packet.... Drop it */
			return 0;
		}
	}

	/* Not found the packet in the cache... Add it to the packet cache */

	j = packet_cache[hash_value].packet_cache_write_index ;	
	packet_cache[hash_value].ip_src[j] = src;
	packet_cache[hash_value].ip_id[j] = id;
	packet_cache[hash_value].ip_hook[j] = hook;
	packet_cache[hash_value].packet_cache_write_index = (j+1)%(MAX_PACKET_CACHE);
	return 1;	
}

u_int8_t	packet_hashfunction(u_int32_t addr)
{
    char value = (char)addr;
    value ^= (char) (addr >> 8); 
    value ^= (char)(addr >> 16);
    value ^= (char)(addr >> 24); 
    return (value % HASHBUCKETS);
}

#endif /* MAODV */
